package nix.bs.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nix.bs.domain.Book;
import nix.bs.domain.Cart;
import nix.bs.domain.CartItem;
import nix.bs.service.BookService;
import nix.servlet.BaseServlet;

public class CartServlet extends BaseServlet {

	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Cart cart = (Cart) request.getSession().getAttribute("cart");
		String bid = request.getParameter("bid");
		Book book = new BookService().load(bid);
		int count = Integer.parseInt(request.getParameter("count"));

		CartItem cartItem = new CartItem();
		cartItem.setBook(book);
		cartItem.setCount(count);

		cart.add(cartItem);

		return "f:/bs/jsps/cart/list.jsp";
	}

	public String clear(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Cart cart = (Cart) request.getSession().getAttribute("cart");
		cart.clear();

		return "f:/bs/jsps/cart/list.jsp";
	}

	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Cart cart = (Cart) request.getSession().getAttribute("cart");
		String bid = request.getParameter("bid");
		cart.delete(bid);

		return "f:/bs/jsps/cart/list.jsp";
	}
}
